| Conditions | 1 |
| Paths | 4 |
| Total Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | var _ = require('lodash'); |
||
| 62 | var keepSpending = function() { |
||
| 63 | return wallet.getBalance().then(function(b) { |
||
| 64 | console.log(b); |
||
| 65 | |||
| 66 | // do a split send |
||
| 67 | return splitSend(Math.min(b[0], value)) |
||
| 68 | .then(function() { |
||
| 69 | // keep sending for as long as possible |
||
| 70 | return keepSpending() |
||
| 71 | .then(function() { |
||
| 72 | // reset retries on success |
||
| 73 | retries = 0; |
||
| 74 | }); |
||
| 75 | }, function(e) { |
||
| 76 | console.error(e.message || e); |
||
| 77 | |||
| 78 | // if TX is too big (or MrSign times out cuz TX is too big to sign) |
||
| 79 | if (e.message.match(/too big/) || e.message.match(/MrSign/)) { |
||
| 80 | // if value reaches <= MINVALUE then we're done(ish) |
||
| 81 | if (value <= MINVALUE) { |
||
| 82 | retries++; |
||
| 83 | |||
| 84 | // retry 3 more times, otherwise done |
||
| 85 | if (retries >= 3) { |
||
| 86 | throw new Error("DONE"); |
||
| 87 | } |
||
| 88 | |||
| 89 | // keep trying |
||
| 90 | return keepSpending(); |
||
| 91 | } else { |
||
| 92 | // halve the value and try again |
||
| 93 | value *= 0.5; |
||
| 94 | console.log('too big, new value:', blocktrail.toBTC(value)); |
||
| 95 | |||
| 96 | return keepSpending(); |
||
| 97 | } |
||
| 98 | } else if (e.message.match(/too low/)) { |
||
| 99 | // halve the value and try again |
||
| 100 | value *= 0.5; |
||
| 101 | console.log('too big, new value:', blocktrail.toBTC(value)); |
||
| 102 | |||
| 103 | return keepSpending(); |
||
| 104 | } else if (e.message.match(/All usable unspent/)) { |
||
| 105 | // halve the value and try again, but with a timeout |
||
| 106 | value *= 0.5; |
||
| 107 | console.log('too big, new value:', blocktrail.toBTC(value)); |
||
| 108 | |||
| 109 | var deferred = q.defer(); |
||
| 110 | |||
| 111 | setTimeout(function() { |
||
| 112 | deferred.resolve(); |
||
| 113 | }, 11000); |
||
| 114 | |||
| 115 | return deferred.promise.then(function() { |
||
| 116 | return keepSpending(); |
||
| 117 | }); |
||
| 118 | } |
||
| 119 | |||
| 120 | throw e; |
||
| 121 | }); |
||
| 122 | }); |
||
| 123 | }; |
||
| 124 | |||
| 131 |